package cn.exrick.xboot.modules.commonlyUsed.serviceimpl;


import cn.exrick.xboot.base.param.BaseParam;
import cn.exrick.xboot.modules.commonlyUsed.dao.mapper.OrgCommonlyMapper;
import cn.exrick.xboot.modules.commonlyUsed.dto.CommonlyDTO;
import cn.exrick.xboot.modules.commonlyUsed.dto.CommonlyVO;
import cn.exrick.xboot.modules.commonlyUsed.entity.CommonlyUsed;
import cn.exrick.xboot.modules.commonlyUsed.entity.OrgCommonly;
import cn.exrick.xboot.modules.commonlyUsed.service.IOrgCommonlyService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Slf4j
@Service
@Transactional
public class IOrgCommonlyServiceImpl extends ServiceImpl<OrgCommonlyMapper, OrgCommonly> implements IOrgCommonlyService {

    @Autowired
    OrgCommonlyMapper orgCommonlyMapper;

    @Override
    public List<CommonlyVO> orgCommonlyList(CommonlyDTO commonlyDTO) {

        List<CommonlyVO> orgCommonlyList = orgCommonlyMapper.orgCommonlyList(commonlyDTO.getOrgId());

        List<CommonlyVO> newList = new ArrayList();
        for (CommonlyVO orgCommonly : orgCommonlyList) {
            if (orgCommonly.getOrgId() != null) {
                orgCommonly.setIsOpen(1);
            } else {
                orgCommonly.setIsOpen(0);
            }
            newList.add(orgCommonly);
        }

        return newList;
    }

    @Override
    public List<CommonlyVO> openList(CommonlyDTO commonlyDTO) {
        List<CommonlyVO> orgCommonlyList = orgCommonlyMapper.openList(commonlyDTO.getOrgId());
        return orgCommonlyList;
    }


    @Override
    public boolean InsertOrUpdateCommonly(CommonlyDTO commonlyDTO) {

        List<CommonlyUsed> paramList = commonlyDTO.getCommonlyUseds();


        if (paramList != null && !paramList.isEmpty()) {
            for (CommonlyUsed commonlyUsed : paramList) {

                String id = commonlyUsed.getId();
                String orgid = commonlyDTO.getOrgId();
                LambdaQueryWrapper<OrgCommonly> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(OrgCommonly::getOrgId, commonlyDTO.getOrgId());
                queryWrapper.eq(OrgCommonly::getFunctionCode, commonlyUsed.getId());
                OrgCommonly isExist = orgCommonlyMapper.selectOne(queryWrapper);
                if (commonlyUsed.getIsOpen() == 1) {
                    if (null == isExist) {
                        OrgCommonly orgCommonly = new OrgCommonly();
                        orgCommonly.setId(UUID.randomUUID().toString());
                        orgCommonly.setOrgId(commonlyDTO.getOrgId());
                        orgCommonly.setFunctionCode(commonlyUsed.getId());
                        orgCommonlyMapper.insert(orgCommonly);
                    }
                } else if (commonlyUsed.getIsOpen() == 0) {
                    if (null != isExist) {
                        orgCommonlyMapper.delete(queryWrapper);
                    }
                }
            }
            return true;
        }
        return false;
    }
}

